import { inject, observer } from "mobx-react";
import React from "react";
import { StyleSheet, View } from "react-native";
import navigator from "../utils/navigator";
import BasePage from "./BasePage";
import { logger } from "../utils/util";
import { API } from "../configs";

/**
 * 启动页
 */
@inject("userStore", "appStore")
@observer
export default class SplashPage extends BasePage {

  constructor(props) {
    super(props);
    this.state = {};
  };

  async componentDidMount() {
    super.componentDidMount();
    logger("进入启动页");
    let config = await API();
    logger("当前地址", config.api);
    axios.defaults.baseURL = config.api;
    this._checkUserToken();
  }

  _checkUserToken = async () => {
    try {
      const { setValue } = this.props.userStore;
      setValue("recordingNo", "");
      setValue("playingNo", "");
      const loginInfo = await storage.load({
        key: "session",
      }).catch(logger);
      logger("登录信息", loginInfo);

      const topList = await storage.load({
        key: "topList",
      }).catch(logger);
      setValue("topList", topList);

      const uploadFailList = await storage.load({
        key: "uploadFailList",
      }).catch(logger);
      setValue("uploadFailList", uploadFailList);

      const errorLog = await storage.load({
        key: "errorLog",
      }).catch(logger);
      setValue("errorLog", errorLog);

      //登录信息为空
      if (IsNullOrEmpty(loginInfo)) {
        navigator.resetToPage("Login");
      } else {
        setValue("session", loginInfo);
        navigator.resetToPage("MainPage");
      }
    } catch (e) {
      navigator.resetToPage("Login");
    }
  };

  render() {
    return (
      <View style={styles.container}>

      </View>
    );
  }
}


const styles = StyleSheet.create({
  container: {
    flex: 1,

  },
});
